package com.myexam.dao.impl;

import java.util.List;









import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.myexam.dao.StudentDao;
import com.myexam.model.Score;
import com.myexam.model.Student;
import com.myexam.util.Page;
import com.myexam.util.PageUtil;
@Repository("studentDao")
public class StubentDaoimpl implements StudentDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;
	

	public void addStudent(Student student){
		this.hibernateTemplate.save(student);
	}
	
	@SuppressWarnings("unchecked")
	public List<Student> findStudentByID(String studentID){
		return (List<Student>)this.hibernateTemplate.find("from Student stu where stu.studentID = ?",studentID);
	}
	@Override
	public Student findByStudentID(String studentID) {
		// TODO Auto-generated method stub
		return (Student) this.hibernateTemplate.get(Student.class, studentID);
	}

	/*@Override
	public void updateStudnet(Student student) {
		// TODO Auto-generated method stub
		this.hibernateTemplate.update(student);
		
	}*/

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Student> findStudentByPage(final Page page) {
		// TODO Auto-generated method stub
		List<Student> subjects = (List<Student>)this.hibernateTemplate.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				String hql = "from Student stu";
				List<Student> list = PageUtil.getList(session,hql,page);
				return list;
			}
		});
		return subjects;
	}
	
	@Override
	public int findStudentCount() {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.find("from Student stu").size();
	}
	
	@Override
	public void addScore(Score score) {
		// TODO Auto-generated method stub
		this.hibernateTemplate.save(score);
	}
	
	/*@SuppressWarnings("unchecked")
	public List<Student> likeQueryByName(final Page page){
			
		List<Student> students = this.hibernateTemplate.executeFind(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				String hql = "from Student stu";
				List<Student> list = PageUtil.getList(session, hql,page);
				return list;
			}
		});
		return students;
	}*/

	

}
